<?php
/**
 * 《用车申请》实现类
 *
 * @author 孙晓晔
 * @version $Id: Apply.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CarUtil.class.php');

class Requirement extends CarUtil {
	public $tbl = Constant::tbl_car_requirement;

	public $urgent = array(
		'-' => '请选择',
		'1' => '普通',
		'2' => '重要',
		'3' => '紧急',
			
		'9' => '其它',
	);
	
	public $time = array(
		'-' => '请选择',
		'08:00' => '上午 08:00',
		'09:00' => '上午 09:00',
		'09:30' => '上午 09:30',
		'13:00' => '下午 13:00',
		'13:30' => '下午 13:30',		
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待派车',
		'3' => '已经派车',
		'4' => '无车可派',

		'5' => '暂停派车',
		'6' => '取消派车',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '用车人',
		't1' => '出车时间',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '申请人',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('urgent_option', $this->urgent);
		$controller->assign_by_ref('time_option', $this->time);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'car/requirement/find');
		return true;
	}

	function _orderby_x($controller, $row, $px = '-') {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$urgent_s = $request['urgent_s'];
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$where = " WHERE 1=1";
		
		if (!empty($urgent_s) && $urgent_s <> '-') {
			$where .= " AND urgent = '" . $urgent_s . "'";
		}

		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 < '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'car/requirement/list');
		return true;
	}

	function _my() {
		if ($this->_audit('77')) {
			return '';
		}

		$emp = $_SESSION['id'];

		return " emp = '" . $emp . "'";
	}

	function _plan($id) {
		if (empty($id)) {
			return false;
		}
		
		$row = Common::id(Constant::tbl_car_plan, $id);
		if (is_array($row)) {
			return sprintf("%s/%s", Name::car($row['car']), Name::emp($row['driver']));
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'car/requirement/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);		

		$urgent = $request['urgent'];
		$t1 = $request['t1'];
		if (empty($t1)) {
			$t1 = sprintf("%s %s", $request['date1'], $request['time1']);
		}
		
		$t2 = $request['t2'];
		if (empty($t2)) {
			$t2 = sprintf("%s %s", $request['date2'], $request['time2']);
		}

		$name = addslashes($request['name']);
		$address = addslashes($request['address']);
		$content = addslashes($request['content']);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, urgent, t1, t2, name, address, content, oper, createtime, modifytime) ";
		$fmt .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $urgent, $t1, $t2, $name, $address, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _notice_1($controller, $request, $id) {
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);
		
		$subject = sprintf('用车需求 %s %s', $row['id'], $row['emp_t']);
		$body = $controller->sunxy($request, 'car/requirement/notice_1');			
		// debug($body);
		
		if ($emp['office'] == '1') {
			$this->_notice_x($row['emp'], '319', $subject, $body);
		}
		
		if ($emp['office'] == '2') {
			$this->_notice_x($row['emp'], '320', $subject, $body);
		}
		
		if ($emp['office'] == '3') {
			$this->_notice_x($row['emp'], '354', $subject, $body);
		}
	}
	
	function _notice_x($emp, $service, $subject, $body) {
		$bb = $this->_list_service($service);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		while (list($k, $to) = each($bb)) {
			// $to = '101';
			// echo $to . "\n";
			$this->_notice($emp, $to, $subject, $body, 1);
		}
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/requirement/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$urgent = $request['urgent'];
		$t1 = $request['t1'];
		if (empty($t1)) {
			$t1 = sprintf("%s %s", $request['date1'], $request['time1']);
		}
		
		$t2 = $request['t2'];
		if (empty($t2)) {
			$t2 = sprintf("%s %s", $request['date2'], $request['time2']);
		}

		$name = addslashes($request['name']);
		$address = addslashes($request['address']);
		
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', urgent = '%s', t1 = '%s', t2 = '%s', name = '%s', address = '%s', ";
		$fmt .= "content = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $urgent, $t1, $t2, $name, $address, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/requirement/copy');
		return true;
	}

	/**
	 * 审核
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/requirement/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$this->_notice_1($controller, $request, $id);
		
		// $this->_notice_9($controller, $request, $id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _notice_9($controller, $request, $id, $status) {
		$row = $this->_view($id);

		if ($status == '2') {
			$subject = sprintf('用车需求 %s 审核通过', $row['id']);
		} else {
			$subject = sprintf('用车需求 %s 审核未通过', $row['id']);
		}
				
		$controller->assign_by_ref('result', $row);

		$body = $controller->sunxy($request, 'car/requirement/notice_9');

		$this->_notice($_SESSION['id'], $row['emp'], $subject, $body, 1);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];		
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/requirement/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['plan_t'] = $this->_plan($row['plan']);
				
		$row['urgent_t'] = $this->urgent[$row['urgent']];
		$row['date'] = $this->_date($row['t1'], $row['t2']);
		
		$row = $this->_view_t($row);
		
		return $row;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$row = $this->id($this->tbl, $id);
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		$controller->assign('result_s', "100");

		$controller->display($request, 'car/requirement/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t1, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(t1, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(t1, '%Y-%m')";
		}

		return $px;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}
		
		return $row;
	}

	/**
	 * 无车可派
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function nocar($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/requirement/nocar');
		return true;
	}

	/**
	 * 无车可派
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function nocarPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '4', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$this->_notice_3($controller, $request, $id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _notice_3($controller, $request, $id) {
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$subject = sprintf('用车需求 %s 无车可派', $row['id']);
		$body = $controller->sunxy($request, 'car/requirement/notice_3');
		
		$this->_notice($_SESSION['id'], $row['emp'], $subject, $body, 1);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function plan($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		require_once(dirname(__FILE__) . '/Plan.class.php');
		$plan = new Plan();
		$data = $plan->_list();
		
		$controller->assign_by_ref('plan_option', $data);

		$controller->display($request, 'car/requirement/plan');
		return true;
	}

	/**
	 * 用车安排
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function planPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$plan = $this->_input($request['plan']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET plan = '%s', content = concat(content, '%s'), status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $plan, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$this->_notice_2($controller, $request, $id, $plan);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _notice_2($controller, $request, $id, $plan_id) {
		$req = $this->_view($id);

		require_once(dirname(__FILE__) . '/Plan.class.php');
		$plan = new Plan();
		$row = $plan->_view($plan_id);
		
		$controller->assign_by_ref('result', $row);

		$subject = sprintf('用车需求 %s 已经派车', $req['id']);
		$body = $controller->sunxy($request, 'car/requirement/notice_2');
			
		$this->_notice($_SESSION['id'], $req['emp'], $subject, $body, 1);
	}

	/**
	 * 直接派车
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function car($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/requirement/car');
		return true;
	}
	
	function _car($controller, $request, $id, $plan) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET plan = '%s', status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $plan, $id);
		// debug($sql);
		$db->query($sql);
		
		$this->_notice_2($controller, $request, $id, $plan);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$status = $request['status'];
		$controller->assign_by_ref('reason', $this->status[$status]);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once(dirname(__FILE__) . '/../emp/Emp.class.php');
		$emp = new Emp();
		$controller->assign_by_ref('emp', $emp->_view($row['emp']));

		$controller->display($request, 'car/requirement/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);		
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status_continue($controller, $request) {
		$this->pretreat($controller);

		return $this->status_restart($controller, $request);
	}

	function status_restart($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$row = $this->id($this->tbl, $id);
		if (empty($row['plan'])) {
			$status = 2;
		} else {
			$status = 3;
		}

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function tj1($controller, $request) {
		$a = array(
			'南山用车申请'=>array('a'=>'328', 'office'=>'1'),
			'沙井用车申请'=>array('a'=>'329', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
						
			$this->tj($controller, $request);
		}
	}
	
	function tj2($controller, $request) {
		$a = array(
			'南山用车申请（周报）'=>array('a'=>'328', 'office'=>'1'),
			'沙井用车申请（周报）'=>array('a'=>'329', 'office'=>'2'),
		);
		
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
			
			$this->tjw($controller, $request);
		}				
	}
	
	function tj($controller, $request) {		
		$bb = $this->_list_email('300');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '用车申请';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}
		
		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'car/requirement/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {		
		$bb = $this->_list_email('300');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$from = $this->email;
		$subject = '用车申请（周报）';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE createtime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);
		
		$body = $this->_tj_html($controller, $request, 'car/requirement/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'emp';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function ns($controller, $request) {
		
		$df = "DATE_FORMAT(t1, '%Y-%m-%d')";
		$dd = date("Y-m-d");
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE office = '1' AND status > 1 AND %s = '%s'";
		$sql = sprintf($fmt, $this->tbl, $df, $dd);
		echo $sql . "\n";
		$result = $db->query($sql);
		
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			$row = $this->_orderby_x($controller, $row);
		
			$data[] = $row;
		}
		
		$from = $this->email;
		$subject = sprintf("%s用车情况", date("Y-m-d"));
	
		$controller->assign_by_ref('result', $data);
		$body = $this->_tj_html($controller, $request, 'car/requirement/ns');
		// echo $body;
	
		$bb = $this->_list_email('170');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
	
		$this->_tj_mail($from, $bb, $subject, $body);
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'car/requirement/_list_c');
		return true;
	}
}
?>
